/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.service;

import cz.muni.dao.CustomerDaoLocal;
import cz.muni.restinterface.CustomerDTO;
import cz.muni.entities.Customer;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.LocalBean;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;

/**
 *
 * @author OSiRiS-PC
 */
@Stateless
@Local(value = CustomerServiceLocal.class)
@LocalBean
public class CustomerService implements CustomerServiceLocal {

    @EJB
    private CustomerDaoLocal customerDao;
    @Resource
    private SessionContext ctx;

    /**
     *
     * @param customer
     * @return
     */
    private CustomerDTO mapToDTO(Customer customer) {
        if (customer == null) {
            return null;
        }
        CustomerDTO customerDTO = new CustomerDTO();

        customerDTO.setCity(customer.getCity());
        customerDTO.setEmail(customer.getEmail());
        customerDTO.setFirstName(customer.getFirstName());
        customerDTO.setId(customer.getId());
        customerDTO.setLastName(customer.getLastName());
        customerDTO.setPhoneNumber(customer.getPhoneNumber());
        customerDTO.setPostalCode(customer.getPostalCode());
        customerDTO.setStreet(customer.getStreet());
        customerDTO.setStreetNumber(customer.getStreetNumber());

        return customerDTO;

    }

    private Customer mapFromDTO(CustomerDTO customerDTO) {
        if (customerDTO == null) {
            return null;
        }
        Customer customer = new Customer();

        customer.setCity(customerDTO.getCity());
        customer.setEmail(customerDTO.getEmail());
        customer.setFirstName(customerDTO.getFirstName());
        customer.setId(customerDTO.getId());
        customer.setLastName(customerDTO.getLastName());
        customer.setPostalCode(customerDTO.getPostalCode());
        customer.setPhoneNumber(customerDTO.getPhoneNumber());
        customer.setStreet(customerDTO.getStreet());
        customer.setStreetNumber(customerDTO.getStreetNumber());


        return customer;
    }

    @Override
    public void create(CustomerDTO customer) {
        System.out.println("customser service create metoda");
        Customer c = mapFromDTO(customer);
        customerDao.create(c);
        customer.setId(c.getId());
    }

    @Override
    public List<CustomerDTO> findByFirstName(String firstName) {

        List<CustomerDTO> customerDTO = new ArrayList();
        List<Customer> customer = customerDao.findByFirstName(firstName);

        for (int i = 0; i < customer.size(); i++) {
            customerDTO.add(mapToDTO(customer.get(i)));
        }
        return customerDTO;
    }

    @Override
    public List<CustomerDTO> findByLastName(String lastName) {
        List<CustomerDTO> customerDTO = new ArrayList();
        List<Customer> customer = customerDao.findByLastName(lastName);

        for (int i = 0; i < customer.size(); i++) {

            customerDTO.add(mapToDTO(customer.get(i)));

        }

        return customerDTO;
    }

    @Override
    public List<CustomerDTO> findByEmail(String email) {
        List<CustomerDTO> customerDTO = new ArrayList();
        List<Customer> customer = customerDao.findByEmail(email);

        for (int i = 0; i < customer.size(); i++) {

            customerDTO.add(this.mapToDTO(customer.get(i)));

        }

        return customerDTO;
    }

    @Override
    public List<CustomerDTO> findByPhoneNumber(String number) {
        List<CustomerDTO> customerDTO = new ArrayList();
        List<Customer> customer = customerDao.findByPhoneNumber(number);

        for (int i = 0; i < customer.size(); i++) {

            customerDTO.add(this.mapToDTO(customer.get(i)));

        }

        return customerDTO;
    }

    @Override
    public void delete(CustomerDTO customer) {
        customerDao.delete(this.mapFromDTO(customer));
    }

    @Override
    public CustomerDTO find(Long id) {
        return this.mapToDTO(customerDao.find(id));
    }

    @Override
    public CustomerDTO update(CustomerDTO customer) {
        return this.mapToDTO(customerDao.update(this.mapFromDTO(customer)));
    }

    @Override
    public List<CustomerDTO> findAll() {
        List<CustomerDTO> customerDTO = new ArrayList();
        List<Customer> customer = customerDao.findAll();

        for (int i = 0; i < customer.size(); i++) {

            customerDTO.add(this.mapToDTO(customer.get(i)));

        }

        return customerDTO;
    }
}
